package cn.hs.controller;

import cn.hs.entity.Free;
import cn.hs.service.FreeService;
import cn.hs.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName FreeController
 * @Description TODO
 * @Author mujiale
 * @Date 2024年10月24日 10:08
 * @Version 1.0
 */
@RestController
@RequestMapping("/free")
public class FreeController {

    @Autowired
    private FreeService freeService;

    @GetMapping
    //查询所有并分页与模糊查询
    public Result select(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize,
            Free free
    ){
        PageInfo<Free> freePageInfo = freeService.selectAll(pageNum, pageSize, free);
        return Result.success(freePageInfo);
    }
    @PostMapping
    //增加
    public Result insert(@RequestBody Free free){
        int num = freeService.insert(free);
        if (num != 0){
            return Result.success("添加成功");
        }else {
            return Result.error("添加失败");
        }
    }
    @PutMapping
    //修改
    public Result update(@RequestBody Free free){
        int num = freeService.update(free);
        if (num != 0){
            return Result.success("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }

    @DeleteMapping("/{id}")
    //删除
    public Result delete(@PathVariable("id") Integer id){
        int num = freeService.delete(id);
        if (num != 0){
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }

    @GetMapping("/findAll")
    //光查询所有
    public Result selectAll(Free free){
        List<Free> select = freeService.select(free);
        return Result.success(select);
    }


    @GetMapping("/findById")
    public Result selectById(Long id){

        Free free = freeService.selectById(id);
        return Result.success(free);
    }
}
